package com.farmUni.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.farmUni.common.BaseContext;
import com.farmUni.entity.Article;
import com.farmUni.entity.Liking;
import com.farmUni.mapper.LikingMapper;
import com.farmUni.service.ArticleService;
import com.farmUni.service.LikingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wxy
 * @since 2024-02-04
 */
@Transactional
@Service
@Slf4j
public class LikingServiceImpl extends ServiceImpl<LikingMapper, Liking> implements LikingService {

    @Autowired
    private ArticleService articleService;

    @Override
    public synchronized Boolean likeToArticle(Liking liking) {
        log.info(liking.toString());
        log.info(BaseContext.getCurrentId().toString());
        try {
            Long currentId = BaseContext.getCurrentId();
            Liking isLike = this.getOne(Wrappers.<Liking>lambdaQuery()
                    .eq(Liking::getArticleId, liking.getArticleId())
                    .eq(Liking::getUserId, currentId)
                    .last("LIMIT 1"));
            if(isLike == null){
                liking.setUserId(currentId);
                //liking.setArticleId()
                //点赞
                log.info("点赞~");
                this.save(liking);
                Article article = articleService.getById(liking.getArticleId());
                if (article != null) {
                    // 增加点赞计数
                    article.setLiking(article.getLiking() + 1);
                    // 保存或更新article实体
                    articleService.updateById(article);
                }
            }else {
                log.info("取消点赞~");
                //取消点赞
                this.remove(Wrappers.<Liking>lambdaQuery()
                        .eq(Liking::getArticleId,liking.getArticleId())
                        .eq(Liking::getUserId,currentId));
                Article article = articleService.getById(liking.getArticleId());
                if (article != null && article.getLiking() >= 0) {
                    // 减少点赞计数
                    article.setLiking(article.getLiking() - 1);
                    // 保存或更新article实体
                    articleService.updateById(article);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

//    @Override
//    public synchronized Boolean likeToArticle(Liking liking) {
//        log.info(liking.toString());
//        log.info(BaseContext.getCurrentOpenId());
//        try {
//            String currentOpenId = BaseContext.getCurrentOpenId();
//            Liking isLike = this.getOne(Wrappers.<Liking>lambdaQuery()
//                    .eq(Liking::getArticleId, liking.getArticleId())
//                    .eq(Liking::getOpenid, currentOpenId)
//                    .last("LIMIT 1"));
//            if(isLike == null){
//                liking.setOpenid(currentOpenId);
//                //liking.setArticleId()
//                //点赞
//                log.info("点赞~");
//                this.save(liking);
//                Article article = articleService.getById(liking.getArticleId());
//                if (article != null) {
//                    // 增加点赞计数
//                    article.setLiking(article.getLiking() + 1);
//                    // 保存或更新article实体
//                    articleService.updateById(article);
//                }
//            }else {
//                log.info("取消点赞~");
//                //取消点赞
//                this.remove(Wrappers.<Liking>lambdaQuery()
//                        .eq(Liking::getArticleId,liking.getArticleId())
//                        .eq(Liking::getOpenid,currentOpenId));
//                Article article = articleService.getById(liking.getArticleId());
//                if (article != null && article.getLiking() >= 0) {
//                    // 减少点赞计数
//                    article.setLiking(article.getLiking() - 1);
//                    // 保存或更新article实体
//                    articleService.updateById(article);
//                }
//            }
//            return true;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
}
